# This Python file uses the following encoding: utf-8
# @author runhey
# github https://github.com/runhey

import cv2
import numpy as np

from module.logger import logger
from module.ocr.base_ocr import BaseCor, OcrMode, OcrMethod
from module.ocr.sub_ocr import Full, Single, Digit, DigitCounter, Duration


class RuleOcr(Digit, DigitCounter, Duration, Single, Full):

    def __init__(self, *args, **kwargs):
        # 初始化父类
        super().__init__(*args, **kwargs)

    def after_process(self, result):
        # 根据模式选择不同的处理方式
        match self.mode:
            case OcrMode.FULL:
                return Full.after_process(self, result)
            case OcrMode.SINGLE:
                return Single.after_process(self, result)
            case OcrMode.DIGIT:
                return Digit.after_process(self, result)
            case OcrMode.DIGITCOUNTER:
                return DigitCounter.after_process(self, result)
            case OcrMode.DURATION:
                return Duration.after_process(self, result)
            case _:
                return result

    def ocr(self, image, keyword=None):

        # 根据模式选择不同的OCR方式
        match self.mode:
            case OcrMode.FULL:
                return Full.ocr_full(self, image, keyword)
            case OcrMode.SINGLE:
                return Single.ocr_single(self, image)
            case OcrMode.DIGIT:
                return Digit.ocr_digit(self, image)
            case OcrMode.DIGITCOUNTER:
                return DigitCounter.ocr_digit_counter(self, image)
            case OcrMode.DURATION:
                return Duration.ocr_duration(self, image)
            case _:
                return None

    def coord(self) -> tuple:
        """
        获取一个区域，随机返回一个坐标
        :return:
        """
        area = None
        # 如果模式为FULL，则返回整个区域的坐标
        if self.mode == OcrMode.FULL:
            area = self.area
        # 否则，返回ROI区域的坐标
        else:
            area = self.roi

        # 获取区域的坐标
        x, y, w, h = self.area
        # 在区域的x轴上随机生成一个坐标
        x = np.random.randint(x, x + w)
        # 在区域的y轴上随机生成一个坐标
        y = np.random.randint(y, y + h)
        # 返回生成的坐标
        return x, y


if __name__ == "__main__":
    pass
